home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / dns / ipv6.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  3.2 KB  |  129 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''IPv6 helper functions.'''
  5. import re
  6. import dns.exception as dns
  7. import dns.ipv4 as dns
  8. _leading_zero = re.compile('0+([0-9a-f]+)')
  9.  
  10. def inet_ntoa(address):
  11.     """Convert a network format IPv6 address into text.
  12.  
  13.     @param address: the binary address
  14.     @type address: string
  15.     @rtype: string
  16.     @raises ValueError: the address isn't 16 bytes long
  17.     """
  18.     if len(address) != 16:
  19.         raise ValueError, 'IPv6 addresses are 16 bytes long'
  20.     
  21.     hex = address.encode('hex_codec')
  22.     chunks = []
  23.     i = 0
  24.     l = len(hex)
  25.     while i < l:
  26.         chunk = hex[i:i + 4]
  27.         m = _leading_zero.match(chunk)
  28.         if m is not None:
  29.             chunk = m.group(1)
  30.         
  31.         chunks.append(chunk)
  32.         i += 4
  33.     best_start = 0
  34.     best_len = 0
  35.     start = -1
  36.     last_was_zero = False
  37.     for i in xrange(8):
  38.         if chunks[i] != '0':
  39.             if last_was_zero:
  40.                 end = i
  41.                 current_len = end - start
  42.                 if current_len > best_len:
  43.                     best_start = start
  44.                     best_len = current_len
  45.                 
  46.                 last_was_zero = False
  47.             
  48.         last_was_zero
  49.         if not last_was_zero:
  50.             start = i
  51.             last_was_zero = True
  52.             continue
  53.     
  54.     if last_was_zero:
  55.         end = 8
  56.         current_len = end - start
  57.         if current_len > best_len:
  58.             best_start = start
  59.             best_len = current_len
  60.         
  61.     
  62.     if best_len > 0:
  63.         if best_start == 0:
  64.             pass
  65.         None if (best_len == 6 or best_len == 5) and chunks[5] == 'ffff' else chunks[5] == 'ffff'
  66.         hex = ':'.join(chunks[:best_start]) + '::' + ':'.join(chunks[best_start + best_len:])
  67.     else:
  68.         hex = ':'.join(chunks)
  69.     return hex
  70.  
  71. _v4_ending = re.compile('(.*):(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$')
  72. _colon_colon_start = re.compile('::.*')
  73. _colon_colon_end = re.compile('.*::$')
  74.  
  75. def inet_aton(text):
  76.     '''Convert a text format IPv6 address into network format.
  77.     
  78.     @param text: the textual address
  79.     @type text: string
  80.     @rtype: string
  81.     @raises dns.exception.SyntaxError: the text was not properly formatted
  82.     '''
  83.     if text == '::':
  84.         text = '0::'
  85.     
  86.     m = _v4_ending.match(text)
  87.     if m is not None:
  88.         text = '%s:%04x:%04x' % (m.group(1), int(m.group(2)) * 256 + int(m.group(3)), int(m.group(4)) * 256 + int(m.group(5)))
  89.     
  90.     m = _colon_colon_start.match(text)
  91.     if m is not None:
  92.         text = text[1:]
  93.     else:
  94.         m = _colon_colon_end.match(text)
  95.         if m is not None:
  96.             text = text[:-1]
  97.         
  98.     chunks = text.split(':')
  99.     l = len(chunks)
  100.     if l > 8:
  101.         raise dns.exception.SyntaxError
  102.     
  103.     seen_empty = False
  104.     canonical = []
  105.     for c in chunks:
  106.         if c == '':
  107.             if seen_empty:
  108.                 raise dns.exception.SyntaxError
  109.             
  110.             seen_empty = True
  111.             for i in xrange(0, (8 - l) + 1):
  112.                 canonical.append('0000')
  113.             
  114.         lc = len(c)
  115.         if lc > 4:
  116.             raise dns.exception.SyntaxError
  117.         
  118.         if lc != 4:
  119.             c = '0' * (4 - lc) + c
  120.         
  121.         canonical.append(c)
  122.     
  123.     if l < 8 and not seen_empty:
  124.         raise dns.exception.SyntaxError
  125.     
  126.     text = ''.join(canonical)
  127.     return text.decode('hex_codec')
  128.  
  129.